package kingdom;

import java.util.ArrayList;
import java.util.List;

import entity.Position;
import fractal.Terrain;
import fractal.TerrainMap;
import fractal.TerrainType;

public class Kingdom
{
	protected List<City> _CityList;
	protected List<Position> _TerrainList;
	protected TerrainMap _Map;
	protected int _EmpireId;
	protected Echelle _EchelleTemp;

	public Kingdom(TerrainMap pMap, int pEmpireId)
	{
		_CityList = new ArrayList<City>();
		_TerrainList = new ArrayList<Position>();
		_Map = pMap;
		_EmpireId = pEmpireId;
		// instanciatin des echelle;
		_EchelleTemp = new Echelle();
	}

	public List<City> getCityList()
	{
		return _CityList;
	}

	public List<Position> getTerrainList()
	{
		return _TerrainList;
	}

	public int getNbTerritoire()
	{
		return _TerrainList.size();
	}

	public double getDensite()
	{
		return (double) getNbTerritoire() / (double) _CityList.size();
	}

	public Echelle getEchelleTemp()
	{
		return _EchelleTemp;
	}

	public void grandir()
	{
		for (City city : _CityList)
		{
			city.grandir();
		}
		aggrandirTerritoire();
	}

	protected Terrain getTerrain( Position pPos )
	{
		return _Map.getMap()[pPos.getX()][pPos.getY()];
	}

	protected void aggrandirTerritoire()
	{
		List<Position> list = new ArrayList<Position>();
		list.addAll(_TerrainList);
		for (Position p : list)
		{
			Terrain current = getTerrain(p);
			if (current.getIndiceEmpire() == 100)
			{
				current.setIndiceEmpire(150);
				if (getDensite() > 500
						&& TerrainType.PLAT.equals(current.getTerrainType()))
				{
					current.setCity(true);
					getCityList().add(new City());
				}
				if (p.getY() + 1 < _Map.getMap().length)
				{
					aggrandirTerritoire(p.getX(), p.getY() + 1, current);
				}
				if (p.getX() + 1 < _Map.getMap().length)
				{
					aggrandirTerritoire(p.getX() + 1, p.getY(), current);
				}
				if (p.getX() - 1 >= 0)
				{
					aggrandirTerritoire(p.getX() - 1, p.getY(), current);
				}
				if (p.getY() - 1 >= 0)
				{
					aggrandirTerritoire(p.getX(), p.getY() - 1, current);
				}
			}
			else if (current.getIndiceEmpire() < 100)
			{
				int indiceTerrain = indiceTerrain(current);
				current.setIndiceEmpire(current.getIndiceEmpire()
						+ indiceTerrain);
				if (current.getIndiceEmpire() > 100)
				{
					current.setIndiceEmpire(100);
				}
			}
		}
	}

	protected int indiceTerrain( Terrain pTerrain )
	{

		double spefKingdom = _EchelleTemp.getValue(pTerrain.getHauteur());
		return (int) spefKingdom;
		/*
		 * int indiceTerrain = 25; if
		 * (TerrainType.WATER.equals(pTerrain.getTerrainType())) { indiceTerrain
		 * = 10; } else if
		 * (TerrainType.MOUNTAIN.equals(pTerrain.getTerrainType())) {
		 * indiceTerrain = 0; } else if
		 * (TerrainType.CITY.equals(pTerrain.getTerrainType())) { indiceTerrain
		 * = 100; } return indiceTerrain;
		 */
	}

	protected void aggrandirTerritoire( int i , int j , Terrain current )
	{
		Terrain next = _Map.getMap()[i][j];
		if (next.getEmpire() == -1)
		{
			int indice = indiceTerrain(current);
			if (indice != 0)
			{
				next.setEmpire(_EmpireId);
				next.setIndiceEmpire(indice);
				_TerrainList.add(new Position(i, j));
			}
		}
	}
}
